import os
import sys
import django

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.extend([BASE_DIR, ])
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "finance_c2c.settings")
django.setup()
from apps.sales_man.models import ChannelProduct
from apps.sales_man.models import Channel
from apps.product.models import Product

# 更新价格
d = """
UPDATE   `finance_c2c_new`.`order_priceupdate` set `price` = 50, `leader_price` =30 ,`leader_leader_price` =0  where `channel_product_id` IN ( SELECT `id`  FROM `finance_c2c_new`.`sales_man_channelproduct` where `product_id`  in (SELECT `id`  FROM `finance_c2c_new`.`product_product` WHERE `parent_product_id` IN (35)) and `channel_id` = 10)
"""


class AddChannel(object):
    def __init__(self, tel, username, name):
        self.tel = tel
        self.username = username
        self.name = name
        self.channel = None

    def add_channel(self):
        channel = Channel.objects.create(
            tel=self.tel,
            name=self.name,
            username=self.username
        )
        self.channel = channel

    def update_channel_product(self):
        product_queryset = self.select_product()
        for i in product_queryset:
            ChannelProduct.objects.get_or_create(
                channel=self.channel,
                product=i
            )

    # def select_product_all(self):
    #     return Product.objects.filter(status=True)

    def select_product(self):
        """
        浦发：22
        交通： 3
        上海： 24
        光大： 15
        兴业： 	8
        华夏： 	35
        民生： 	23
        中原： 	12
        车抵贷： 	17
        天津浦发： 	54
        :return:
        """
        # product_id_list = [
        #     54
        # ]
        product_id_list = [
            22, 3, 15, 24
        ]
        child_product1 = Product.objects.get(id=24).product_set.all()
        # child_product2 = Product.objects.get(id=35).product_set.all()
        base_product = Product.objects.filter(id__in=product_id_list)
        # return base_product
        return child_product1 | base_product
        # return child_product1 | base_product | child_product2


def add_channel():
    tel, username, name = "张总谭文军", "张总谭文军", "张总谭文军"
    add_channel_obj = AddChannel(tel, username, name)
    add_channel_obj.add_channel()
    add_channel_obj.update_channel_product()


def update_channel_product():
    product_id_list = [i for i in range(55, 74)]
    if product_id_list:
        products = Product.objects.filter(id__in=product_id_list)
        products.update(list_show=True)
        # else:
        #     products = Product.objects.all()
        # for i in products:
        #     ChannelProduct.objects.get_or_create(
        #         channel_id=1,
        #         product=i
        #     )


if __name__ == '__main__':
    add_channel()
    # update_channel_product()
